package com.jkoss.wine.weekly.service.impl;

import com.jkoss.common.util.CommonUtil;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.MessageNotification;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.system.mapper.MerchantEmployeesMapper;
import com.jkoss.wine.system.mapper.MessageNotificationMapper;
import com.jkoss.wine.system.mapper.RoleMapper;
import com.jkoss.wine.weekly.entity.Monthly;
import com.jkoss.wine.weekly.entity.Weekly;
import com.jkoss.wine.weekly.mapper.MonthlyMapper;
import com.jkoss.wine.weekly.service.IMonthlyService;
import com.jkoss.wine.weekly.vo.MonthlyVo;
import com.jkoss.wine.weekly.vo.WeeklyVo;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 月报 服务实现类
 * 
 * @Author Jason
 * @Version 1.0, 2018-12-05
 * @See
 * @Since com.jkoss.wine.weekly.service.impl
 * @Description: TODO
 */
@Service
public class MonthlyServiceImpl extends ServiceImpl<MonthlyMapper, Monthly> implements IMonthlyService {
	@Autowired
	private MessageNotificationMapper messageNotificationMapper;
	@Autowired
	private MerchantEmployeesMapper merchantEmployeesMapper;
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public Page selectVoByPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoByPage(page, wrapper));
		return page;
	}

	@Override
	public MonthlyVo selectVoById(String id) {
		return baseMapper.selectVoById(id);
	}

	@Override
	public boolean insert(Monthly entity) {
		Role role = roleMapper.selectById(entity.getRid());
		Wrapper wrapper = Condition.create().ne("r.del", 1).ne("me.del", 1).eq("r.id", role.getPid());
		List<MerchantEmployees> merchantEmployees = merchantEmployeesMapper.selectEmployeesByRole(wrapper);
		if (!CommonUtil.isBlank(merchantEmployees)) {
			List list = new ArrayList();
			for (MerchantEmployees merchantEmployees2 : merchantEmployees) {
				MessageNotification messageNotification = new MessageNotification();
				// 员工id
				messageNotification.setMeid(merchantEmployees2.getEmployeeId());
				// 标题
				messageNotification.setTitle("周报未读提醒");
				// 内容
				// messageNotification.setContent(entity.get + ",未读提醒");
				// 是否已读：0-未读、1-已读
				messageNotification.setIsread(0);
				// 添加时间
				messageNotification.setAddTime(CommonUtil.getDate());
				// 是否删除
				messageNotification.setDel(0);
				// 类型：1-通知公告未读提醒、2-周报、3-月报未读及未提交提醒、4-新客户资源分配提醒、5-资源客户未沟通提醒
				messageNotification.setType(3);
				// 操作人
				messageNotification.setOperator(entity.getOperator());
				list.add(messageNotification);
			}
			messageNotificationMapper.insertBatch(list);
		}
		return super.insert(entity);
	}
}
